package com.brillilab.starter.controller.aio.topics;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aliyun.openservices.shade.org.apache.commons.lang3.ObjectUtils.Null;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.topics.Topic;
import com.brillilab.domain.po.topics.TopicDirection;
import com.brillilab.domain.po.topics.TopicMember;
import com.brillilab.domain.vo.topics.TopicDirectionAddVo;
import com.brillilab.domain.vo.topics.TopicDirectionEditVo;
import com.brillilab.domain.vo.topics.TopicDirectionsVo;
import com.brillilab.domain.vo.topics.TopicTargetThumbnailVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.topics.ITopicDirectionService;
import com.brillilab.service.core.topics.ITopicMemberService;
import com.brillilab.service.core.topics.ITopicService;
import com.brillilab.service.logic.topics.TopicLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

/**
 * 课题Controller
 * 
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/topicDirection")
public class TopicDirectionController {
	@Resource
	private UsersLogic usersLogic;
	@Autowired
	private TopicLogic topicLogic;
	@Resource
	private ITopicDirectionService topicDirectionService;
	@Resource
	private ITopicService topicService;
	@Resource
	private ITopicMemberService topicMemberService;
	
	/**
	 * 新建研究内容
	 * 
	 * @return
	 */
	@PostMapping("/add")
	public ResponseVo directionAdd(@RequestBody TopicDirectionAddVo addVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(addVo!=null && addVo.getTopicId()!=null
				&& StringUtils.isNotBlank(addVo.getName()) && StringUtils.isNotBlank(token),"请求参数缺失!");
		
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Topic topic = topicService.selectById(addVo.getTopicId());
		boolean isPower = topicLogic.topicEditPower(topic, userInfo);
		if(!isPower) {
			TopicMember topicMember= topicMemberService.getTopicMember(addVo.getTopicId(),userInfo.getLabMemberId());
			isPower = topicMember!=null;
		}
		Assert.isTrue(isPower, "你没有权限操作该课题");

		addVo.setLabId(userInfo.getLabId());
		TopicDirection topicDirection = topicDirectionService.addTopicDirection(addVo);
		Map<String, Object> map = new HashMap<>();
		map.put("topicDirectionId", topicDirection.getId());
		map.put("name", topicDirection.getName());
		map.put("figure", topicDirection.getFigure());
		return ResponseVo.success(map);
	}
	
	/**
	 * 删除研究内容
	 * 
	 * @return
	 */
	@GetMapping("/delete")
	public ResponseVo directionDelete(Long topicDirectionId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(topicDirectionId!=null && StringUtils.isNotBlank(token),"请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicDirection direction = topicDirectionService.getById(topicDirectionId);
		Topic topic = topicService.selectById(direction.getTopicId());
		boolean isPower = topicLogic.topicEditPower(topic, userInfo);
		Assert.isTrue(isPower, "你没有权限操作该课题");
		topicDirectionService.delete(direction);
		return ResponseVo.success();
	}

	/**
	 * 编辑研究内容
	 * 
	 * @return
	 */
	@PostMapping("/edit")
	public ResponseVo directionEdit(@RequestBody TopicDirectionEditVo editVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(editVo!=null && editVo.getTopicDirectionId()!=null
				&& StringUtils.isNotBlank(editVo.getName()) && StringUtils.isNotBlank(token),"请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicDirection direction = topicDirectionService.getById(editVo.getTopicDirectionId());
		Topic topic = topicService.selectById(direction.getTopicId());
		boolean isPower = topicLogic.topicEditPower(topic, userInfo);
		Assert.isTrue(isPower, "你没有权限操作该课题");
		
		topicDirectionService.editTopicDirection(editVo);
		return ResponseVo.success();
	}

	
	/**
	 * 获取研究内容列表
	 * 
	 * @return
	 */
	@Deprecated
	@GetMapping("/list")
	public ResponseVo directionList(@RequestParam Long topicId,
			@RequestParam(defaultValue = "1") Integer  pageNum,
			@RequestParam(defaultValue = "9")Integer pageSize,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Topic topic = topicService.selectById(topicId);
		Assert.isTrue(topic!=null && BoolEnum.FALSE.getValue().equals(topic.getIsDelete()), "课题已被删除");
		
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicDirectionsVo vo = topicLogic.selectPageVo(pageNum,pageSize,topicId,userInfo);
		return ResponseVo.success(vo.getPage());
	}
	
	/**
	 * 获取研究内容列表对象(包含课题名称)
	 * 
	 * @return
	 */
	@GetMapping("/listVo")
	public ResponseVo directionListVo(@RequestParam Long topicId,
			@RequestParam(defaultValue = "1") Integer  pageNum,
			@RequestParam(defaultValue = "9")Integer pageSize,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Topic topic = topicService.selectById(topicId);
		Assert.isTrue(BoolEnum.FALSE.getValue().equals(topic.getIsDelete()), "课题已删除");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicDirectionsVo vo = topicLogic.selectPageVo(pageNum,pageSize,topicId,userInfo);
		return ResponseVo.success(vo);
	}
	
	/**
	 * 获取研究内容中实验流缩略图列表
	 * 
	 * @return
	 */
	@GetMapping("/thumbnail")
	public ResponseVo thumbnailList(Long topicDirectionId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		TopicTargetThumbnailVo thumbnailVo = topicLogic.getThumbnailList(topicDirectionId);
		return ResponseVo.success(thumbnailVo);
	}

	/**
	 * 判断当前用户是否可以新增研究内容/实验流/实验目标
	 * @param topicId
	 * @param token
	 * @return
	 */
	@GetMapping("/canAdd")
	public ResponseVo canAdd(Long topicId,
							 @RequestHeader(name = StarterConstant.TOKEN) String token){
		Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失!");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		boolean rs = topicLogic.topicDirectionCanAdd(topicId,userInfo);
		return ResponseVo.success(rs);
	}
}
